package org.lanqiao.tjut.testdriver;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Test;
import org.lanqiao.tjut.driver.GetdriverFactory;
import org.lanqiao.tjut.entity.StudentEntity;

public class Methods {
	Scanner input = new Scanner(System.in);

	// 此类存储着方法便于调用
	// 这个是用户登录的方法，用于检测用户是学生还是老师登陆，
	// 并判断账号和密码是否正确，能否登入。
	// 登陆是与数据库中的内容做对比，若相同就可以进，若不同就不能进
	// 第一步要从教师端往数据库录入信息
	@Test
	public void login() {
		System.out.println("************************");
		System.out.println("*       1.学生登陆      *");
		System.out.println("*       2.教师登陆      *");
		System.out.println("*       3.退出          *");
		System.out.println("*************************");
		System.out.print("请输入登录形式(输入1,或2或3):");
		int i = input.nextInt();
		if (i == 1) {
			List list = null;
			do {
				System.out.print("请输入学号:");
				String stuid2 = input.next();
				System.out.print("请输入密码:");
				String stups2 = input.next();
				String strSQLL = "Select * from student where studentid=?and studentpassword=?";
				list = (List) GetdriverFactory.DriverFactory().query1(strSQLL, new BeanListHandler(StudentEntity.class),
						new Object[] { stuid2, stups2 });
				if (list != null && list.size() != 0) {
					this.Stulogin(stuid2);
				}
			} while (list == null || list.size() == 0);

		} else if (i == 2) {
			// 这是一个死循环，直到输入正确才能出去，否则一直循环

			while (true) {
				String tea = "admin";
				String teapw = "123456";
				System.out.print("请输入用户名:");
				String teacherid = input.next();

				System.out.print("请输入密码:");
				String teacherpassword = input.next();

				if (tea.equals(teacherid) && teapw.equals(teacherpassword)) {
					System.out.println("登陆成功");
					this.dorevise();
					break;
				} else {
					System.out.println("输入不成功，请重新输入");
				}
			}
		} else if (i == 3) {

		}
	}

	// 进入教师端后，有4种功能，分别是增删改查
	public void dorevise() {
		int i = 7;
		do {
			System.out.println("************************");
			System.out.println("*      1.添加学生信息");
			System.out.println("*      2.删除学生信息");
			System.out.println("*      3.修改学生信息");
			System.out.println("*      4.查询学生信息");
			System.out.println("*      5.退出或按任意键选择退出");
			System.out.println("************************");
			System.out.print("(输入1-4之间的数)请选择功能菜单:");
			i = input.nextInt();
			doSelect(i);
		} while (i != 5);
	}

	private void doSelect(int i) {
		switch (i) {
		case 1:
			// 选择了添加学生信息的功能
			System.out.println("请输入学生信息");

			System.out.print("请输入学生学号:");
			int stuid = input.nextInt();
			System.out.print("请输入学生密码:");
			String stups = input.next();
			System.out.print("请输入学生姓名:");
			String stuname = input.next();
			System.out.print("请输入学生年龄:");
			int stuage = input.nextInt();
			System.out.print("请输入学生地址:");
			String stuaddr = input.next();
			System.out.print("请输入学生电话:");
			int stutel = input.nextInt();
			System.out.print("请输入学生性别:");
			String stusex = input.next();
			// 将此信息导入数据库
			// 写sql语句
			String strSQL = "insert into Student(studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex) values(?,?,?,?,?,?,?)";
			int n = GetdriverFactory.DriverFactory().insert1(strSQL,
					new Object[] { stuid, stups, stuname, stuage, stuaddr, stutel, stusex });
			System.out.println(n > 0 ? "信息导入成功" : "信息导入失败");
			String strSQL1 = "select studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex from Student order by throne desc";
			List list = (List) GetdriverFactory.DriverFactory().query(strSQL1, new MapListHandler());
			printList(list);
			break;
		case 2:
			// 选择了删除学生信息的功能
			// 首先显示所有学生信息
			String strSQL2 = "select studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex from Student order by throne desc";
			List list1 = (List) GetdriverFactory.DriverFactory().query(strSQL2, new MapListHandler());
			printList(list1);
			System.out.print("请选择您要删除的学号:");
			int b = input.nextInt();
			// 把学号对应的那一行的标志位改为0
			String strSQL3 = "update student set isflag=0 where studentid=?";
			int m = GetdriverFactory.DriverFactory().update1(strSQL3, new Object[] { b });
			String strSQL4 = "select studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex from Student where isflag=1 order by throne desc";
			List list2 = (List) GetdriverFactory.DriverFactory().query(strSQL4, new MapListHandler());
			printList(list2);
			break;
		case 3:
			// 选择修改学生信息的功能
			// 先显示所有信息
			String strSQL5 = "select studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex from Student order by throne desc";
			List list3 = (List) GetdriverFactory.DriverFactory().query(strSQL5, new MapListHandler());
			printList(list3);
			System.out.print("请输入您的学号:");
			int c = input.nextInt();
			System.out.print("请输入新的学号:");
			int stuid1 = input.nextInt();
			System.out.print("请输入新的密码:");
			String stups1 = input.next();
			System.out.print("请输入新的姓名:");
			String stuname1 = input.next();
			System.out.print("请输入新的年龄:");
			int stuage1 = input.nextInt();
			System.out.print("请输入新的地址:");
			String stuadd = input.next();
			System.out.print("请输入新的电话:");
			int stutel1 = input.nextInt();
			System.out.print("请输入新的性别:");
			String stusex1 = input.next();
			String strSQL6 = "update student set studentid=?,studentpassword=?,studentname=?,studentage=?,studentaddress=?,studenttel=?,studentsex=? where studentid=?";
			int o = GetdriverFactory.DriverFactory().update1(strSQL6,
					new Object[] { stuid1, stups1, stuname1, stuage1, stuadd, stutel1, stusex1, c });
			System.out.println(o > 0 ? "更改成功" : "更改失败");
			break;
		case 4:
			// 查询学生信息
			System.out.println("a:查询所有学生信息");
			System.out.println("b:根据指定学号查询单个学生信息");
			System.out.println("c:根据姓名模糊查找学生信息");
			System.out.println("d:根据地址模糊查找学生信息");
			System.out.println("e:根据年龄段查询学生信息");
			System.out.print("(输入a,b,c,d,e)请选择查询方式:");
			String t = input.next();
			if (t.equals("a")) {
				String strSQL7 = "select studentid,studentname,studentage,studentaddress,friend,studenttel,studentsex,throne  from Student order by throne desc";
				List list4 = (List) GetdriverFactory.DriverFactory().query(strSQL7, new MapListHandler());
				printList(list4);
			} else if (t.equals("b")) {
				System.out.print("请输入要查询的学号:");
				int p = input.nextInt();
				String strSQL7 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend from Student where studentid=? ";
				List list5 = (List) GetdriverFactory.DriverFactory().query1(strSQL7, new MapListHandler(),
						new Object[] { p });
				printList(list5);
			} else if (t.equals("c")) {
				System.out.print("请输入要查询的姓氏:");
				String p = input.next();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend from Student where studentname like ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { "%" + p + "%" });
				printList(list6);
			} else if (t.equals("d")) {
				System.out.println("请输入要查询的地址:");
				String p = input.next();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend from Student where studentaddress like ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { "%" + p + "%" });
				printList(list6);
			} else if (t.equals("e")) {
				System.out.println("请输入要查询的年龄段");
				System.out.println("请输入年龄最小值");
				int min = input.nextInt();
				System.out.println("请输入年龄最大值");
				int max = input.nextInt();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend from Student where studentage between ? and ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { min, max });
				printList(list6);
			}

			break;

		case 5:
			// 退出
			break;
		default:
			break;
		}
	}

	public void Stulogin(String stuid) {
		int i = 11;
		do {
			System.out.println("****************************************");
			System.out.println("               1.查询个人信息              ");
			System.out.println("               2.修改密码             ");
			System.out.println("               3.查询同学信息              ");
			System.out.println("               4.添加好友              ");
			System.out.println("               5.显示我的好友信息              ");
			System.out.println("               6.删除好友信息              ");
			System.out.println("               7.修改好友备注             ");
			System.out.print("请输入功能菜单（输入0-7):");
			i = input.nextInt();
			doSelect1(i, stuid);
		} while (i != 11);

	}

	private void doSelect1(int i, String stuid) {
		switch (i) {
		case 1:
			String strSQL = "select studentid,studentpassword,studentname,studentage,studentaddress,studenttel,studentsex from Student where studentid=? ";
			List list = (List) GetdriverFactory.DriverFactory().query1(strSQL, new MapListHandler(),
					new Object[] { stuid });
			printList(list);
			break;

		case 2:
			System.out.println("请输入你的原始密码");
			String stups = input.next();
			Boolean isFlag = false;
			String stups1 = "";
			do {
				System.out.println("请输入你的新密码");
				stups1 = input.next();
				System.out.println("请输入你的新密码");
				String stups2 = input.next();
				isFlag = !(stups1.equals(stups2));

			} while (isFlag);
			String strSQL1 = "update student set studentpassword=? where studentid=?";
			int o = GetdriverFactory.DriverFactory().update1(strSQL1, new Object[] { stups1, stuid });
			System.out.println(o > 0 ? "密码更改成功" : "密码更改失败");

			break;

		case 3:
			// 查询学生信息
			System.out.println("a:查询所有学生信息");
			System.out.println("b:根据指定学号查询单个学生信息");
			System.out.println("c:根据姓名模糊查找学生信息");
			System.out.println("d:根据地址模糊查找学生信息");
			System.out.println("e:根据年龄段查询学生信息");
			System.out.print("(输入a,b,c,d,e)请选择查询方式:");
			String t = input.next();
			if (t.equals("a")) {
				String strSQL7 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend  from Student order by throne desc";
				List list4 = (List) GetdriverFactory.DriverFactory().query(strSQL7, new MapListHandler());
				printList(list4);
			} else if (t.equals("b")) {
				System.out.print("请输入要查询的学号:");
				int p = input.nextInt();
				String strSQL7 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend   from Student where studentid=? ";
				List list5 = (List) GetdriverFactory.DriverFactory().query1(strSQL7, new MapListHandler(),
						new Object[] { p });
				printList(list5);
			} else if (t.equals("c")) {
				System.out.print("请输入要查询的姓氏:");
				String p = input.next();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend   from Student where studentname like ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { "%" + p + "%" });
				printList(list6);
			} else if (t.equals("d")) {
				System.out.println("请输入要查询的地址:");
				String p = input.next();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend  from Student where studentaddress like ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { "%" + p + "%" });
				printList(list6);
			} else if (t.equals("e")) {
				System.out.println("请输入要查询的年龄段");
				System.out.println("请输入年龄最小值");
				int min = input.nextInt();
				System.out.println("请输入年龄最大值");
				int max = input.nextInt();
				String strSQL8 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend  from Student where studentage between ? and ? ";
				List list6 = (List) GetdriverFactory.DriverFactory().query1(strSQL8, new MapListHandler(),
						new Object[] { min, max });
				printList(list6);
			}

			break;

		case 4:
			// 添加好友
			// 先把同学信息显示出来
			String strSQL7 = "select studentid,studentname,studentage,studentaddress,studenttel,studentsex,friend  from Student where   friend=0 and studentid!=? order by throne desc";
			List list4 = (List) GetdriverFactory.DriverFactory().query1(strSQL7, new MapListHandler(),
					new Object[] { stuid });
			printList(list4);
			System.out.print("请输入你要添加好友的学号:");
			int friendid = input.nextInt();
			System.out.print("请输入好友备注:");
			String friendnote = input.next();
			String strSQL3 = "update student set friend=1 where studentid=?";
			int n1 = GetdriverFactory.DriverFactory().insert1(strSQL3, new Object[] { friendid });
			// 把信息输入到好友表中
			String strSQL2 = "insert into friends(studentid,friendid,friendnote) values(?,?,?)";
			int n = GetdriverFactory.DriverFactory().insert1(strSQL2, new Object[] { stuid, friendid, friendnote });
			System.out.println("恭喜添加好友成功");
			break;

		case 5:// 查询好友信息
			// 把student表和friend表信息联合起来查询
			String strSQL4 = "select s.studentid,s.studentname,s.studentage,s.studentaddress,s.studentsex,s.studenttel,f. friendnote   from student s,friends f   where s.studentid=f.friendid and f.isfalg=1";
			List list5 = (List) GetdriverFactory.DriverFactory().query(strSQL4, new MapListHandler());
			printList(list5);
			break;

		case 6:// 删除好友信息
			System.out.println("请输入被删除好友的学号:");
			int friendid1 = input.nextInt();
			String strSQL5 = "update student set isflag=0 where studentid=?";
			int m = GetdriverFactory.DriverFactory().update1(strSQL5, new Object[] { friendid1 });

			break;

		case 7:
			// 修改备注
			System.out.println("请输入被修改备注的好友学号");
			int friendid2 = input.nextInt();
			System.out.println("请输入被修改的备注");
			String friendnote2 = input.next();
			String strSQL6 = "update friends set friendnote=? where friendid=?";
			int f = GetdriverFactory.DriverFactory().update1(strSQL6, new Object[] { friendnote2, friendid2 });
			break;

		case 11:
			// 退出
			break;

		default:
			break;
		}
	}

	/**
	 * 使用iterator迭代器遍历list集合
	 */
	public void printList(List list) {
		for (int i = 0; i < list.size(); i++) {
			// 获取list中的map对象
			Map m = (Map) list.get(i);
			// 如果是第一个map对象，则先把表头信息打印出来
			if (i == 0) {
				// 打印map对象：字段名
				printMapTitle(m.keySet());
			}
			// 打印map对象表内容信息
			printMap(m);
		}
	}

	/**
	 * 使用keyset迭代遍历输出map集合元素值
	 * 
	 * @param mm
	 *            被遍历的map集合
	 */
	private void printMap(Map mm) {
		// 获取map对象key值的set集合
		Set sk = mm.keySet();
		Iterator itSk = sk.iterator();
		while (itSk.hasNext()) {
			// key 值对象
			Object obKey = itSk.next();
			// value 值对象
			Object obVal = mm.get(obKey);
			System.out.print("\t" + "              " + obVal.toString());
		}
		// 换行
		System.out.println();
	}

	/**
	 * 打印表头信息
	 * 
	 * @param sk
	 *            表头信息set集合对象
	 */
	private void printMapTitle(Set sk) {
		// set集合迭代器
		Iterator itSk = sk.iterator();
		while (itSk.hasNext()) {
			// key 值对象
			Object obKey = itSk.next();
			System.out.print("\t" + "       " + obKey.toString());
		}
		// 换行
		System.out.println();
	}

}
